<template>
  <div>
    <el-drawer
      title="教学计划"
      :visible.sync="drawer"
      :direction="direction"
      :before-close="handleClose"
    >
      <el-form
        ref="form"
        :rules="rules"
        :model="form"
        label-width="100px"
        style="padding-right: 20px"
      >
        <el-form-item label="类型" prop="type">
          <el-radio-group v-model="form.type">
            <el-radio label="1">理论课</el-radio>
            <el-radio label="2">实验课</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="课时" prop="classHour">
          <el-input-number
            v-model="form.classHour"
            :min="1"
            :max="1000"
            size="medium"
            label="排序"
          ></el-input-number>
        </el-form-item>

        <el-form-item label="周次" prop="weeks">
          <el-input-number
            v-model="form.weeks"
            :min="1"
            :max="1000"
            size="medium"
            label="排序"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="授课日期" prop="lessonDate">
          <el-date-picker
            v-model="form.lessonDate"
            type="date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="重点与难点" prop="keyPoint">
          <el-input v-model.trim="form.keyPoint"></el-input>
        </el-form-item>
        <el-form-item label="内容摘要" prop="content">
          <el-input v-model.trim="form.content"></el-input>
        </el-form-item>
        <el-form-item label="作业" prop="busyBook">
          <el-input v-model.trim="form.busyBook"></el-input>
        </el-form-item>
      </el-form>
      <el-button
        type="custom"
        v-preventReClick
        @click="submitForm('form')"
        style="margin-left: 100px"
        >保 存</el-button
      >
    </el-drawer>
  </div>
</template>
<script>
export default {
  data() {
    return {
      drawer: false,
      direction: 'rtl',
      form: {
        busyBook: '', //作业
        classHour: '1', //课时
        courseId: '', //课程id
        chapterId: '', //章节id
        content: '', //内容摘要
        extFiled: '', //扩展字段
        keyPoint: '', //重点与难点
        lessonDate: '', //授课日期
        lessonId: '', //授课计划id
        orderBy: 10, //排序
        type: '1', //类型1-理论课，2-实验课，3…
        weeks: '1', //周次
      },
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
      },
      current: {},
      isAdd: false,
    }
  },
  props: ['curLessonPlan'],
  mounted() {
    this.$bus.$on('showPlan', this.init)
  },
  beforeDestroy() {
    this.$bus.$off('showPlan')
  },
  methods: {
    async init(row) {
      let obj = {
        page: 1,
        limit: 99,
        lessonId: this.curLessonPlan.id,
        chapterId: row.id,
      }
      let res = await this.$store.dispatch('queryEduLessonPlan', obj)
      let data = res.data
      this.isAdd = data.length == 0
      if (this.isAdd) {
        this.form = this.$options.data().form
        this.$delete(this.form, 'id')
      } else {
        this.current = data[0]
        this.form = { ...this.form, ...this.current }
        this.$set(this.form, 'id', this.current.id)
      }
      this.form.chapterId = row.id
      this.drawer = true
    },
    submitForm(form) {
      let { id, courseId } = this.curLessonPlan
      this.form.lessonId = id
      this.form.courseId = courseId
      this.$refs[form].validate(async (valid) => {
        if (valid) {
          let dispatchType = this.isAdd
            ? 'addEduLessonPlan'
            : 'updateEduLessonPlan'
          await this.$store.dispatch(dispatchType, this.form)
          this.successMsg('操作成功')
          this.drawer = false
        }
      })
    },
    handleClose() {
      this.$confirm('是否保存信息？')
        .then((_) => {
          this.submitForm('form')
        })
        .catch((_) => {
          this.drawer = false
        })
    },
  },
}
</script>
<style scoped lang="scss">
</style>